<?php

define('FDMS_WSDL', "http://soap.aldorservices.com/aldorservices03.wsdl" );
define('FDMS_PASSWORD', "10key10");

function getDataForTag($str, $tagname)
{
	$openTag = "<$tagname>";
	$closeTag = "</$tagname>";
	$dataForTag = "";

	$idx1 = stripos( $str, $openTag );
	$idx2 = stripos( $str, $closeTag );

	if($idx===false || $idx2===false) return "";
	if($idx1>$idx2) return "";

	$idx1 += strlen($openTag);
	$dataForTag = substr( $str, $idx1, $idx2-$idx1 );

	return $dataForTag;
}

function getDataAndTags($str, $tagname)
{
	$openTag = "<$tagname>";
	$closeTag = "</$tagname>";
	$dataForTag = "";

	$idx1 = stripos( $str, $openTag );
	$idx2 = stripos( $str, $closeTag );

	if($idx===false || $idx2===false) return "";
	if($idx1>$idx2) return "";

	$idx2 += strlen($closeTag);
	$dataAndTag = substr( $str, $idx1, $idx2-$idx1 );

	return $dataAndTag;
}

class FDMSAPIcalls
{
	public static function getKeystoneFuneralHomes()
	{
		$results = array();

		try {
			$soapClient = new SoapClient( FDMS_WSDL );
		}
		catch(Exception $e)
		{
			echo "exception: $e";
		}

		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectFuneralHomes( FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );
				$payloadxml = simplexml_load_string($payload);
				if(!$payloadxml)
				{
					$results['status'] = "FAIL";
					$results['reason'] = "Malformed response (or response has been altered).";
				}
				else
				{

					$rows = array();

					//echo $payload;
					$datarows = $payloadxml->children();
					foreach( $datarows as $datarow )
					{
						$row['branch_id'] = "".$datarow->FuneralHomeBranchId;
						$row['locale_id'] = "".$datarow->FuneralHomeLocaleId;
						$row['name'] = "".$datarow->FuneralHomeName;
						$row['addr1'] = "".$datarow->FuneralHomeAddress;
						$row['city'] = "".$datarow->FuneralHomeCity;
						$row['state'] = "".$datarow->FuneralHomeState;
						$row['branch_number'] = "".$datarow->FuneralHomeId;

						$rows[] = $row;
					}

					$results['rows'] = $rows;
				}
			}
		}

		return $results;
	}

	public static function getFuneralHomeDetails($fhBranchNum)
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectFuneralHomeByBranchNum( $fhBranchNum, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);

				$payload = str_replace( "&", "and", $payload );

				$payloadxml = simplexml_load_string($payload);
				if(!$payloadxml)
				{
					$results['status'] = "FAIL";
					$results['reason'] = "Malformed response (or response has been altered).";
				}
				else
				{
					$rows = array();
					// and loop thru the homes returned, building an array of associative arrays
					$datarows = $payloadxml->children();
					foreach( $datarows as $datarow )
					{
						$row['branch_id'] = "".$datarow->FuneralHomeBranchId;
						$row['locale_id'] = "".$datarow->FuneralHomeLocaleId;
						$row['name'] = "".$datarow->FuneralHomeName;
						$row['addr1'] = "".$datarow->FuneralHomeAddress;
						$row['city'] = "".$datarow->FuneralHomeCity;
						$row['state'] = "".$datarow->FuneralHomeState;
						$row['zip'] = "".$datarow->FuneralHomeZip;
						$row['phone'] = "".$datarow->FuneralHomePhone;
						$row['url'] = "".$datarow->FuneralHomeWebsite;
						$row['branch_number'] = "".$datarow->FuneralHomeId;
						$rows[] = $row;
					}

					$results['rows'] = $rows;
					$results['status'] = "SUCCEEDED";
				}
			}
		}

		return $results;
	}

	public static function getFuneralDirectors( $fhBranchNum )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectFuneralDirectorsByBranchNum( $fhBranchNum, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );
				$payloadxml = simplexml_load_string($payload);
				if(!$payloadxml)
				{
					$results['status'] = "FAIL";
					$results['reason'] = "Malformed response (or response has been altered).";
				}
				else
				{
					$results['status'] = "SUCCEEDED";
					$rows = array();
					// and loop thru the homes returned, building an array of associative arrays
					$datarows = $payloadxml->children();
					foreach( $datarows as $datarow )
					{
						$row['id'] = "".$datarow->ArrangerId;
						$row['branch_num'] = "".$fhBranchNum;
						$row['full_name'] = "".$datarow->FuneralDirectorFullName;
						$row['funeral_home_name'] = "".$datarow->FuneralHomeName;
						$row['funeral_home_manager'] = "".$datarow->LocationManager;
						$row['funeral_home_id'] = "".$datarow->FuneralHomeID;
						$row['license_number'] = "".$datarow->ArrangerLicenseNumber;
						$row['status'] = "".$datarow->ArrangersStatus;

						$rows[] = $row;
					}
					$results['rows'] = $rows;
				}
			}
		}

		return $results;
	}

	public static function getCasesForDirector( $_directorId )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectCasesByArrangerId( $_directorId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();
				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$count++;

					$row['funeral_director_id'] = $_directorId;
					$row['FDMS_case_num'] = getDataForTag( $raw_row, "DecedentCaseNumber");
					$row['decedent_fname'] = getDataForTag( $raw_row, "DecedentFirstName");
					$row['decedent_mname'] = getDataForTag( $raw_row, "DecedentMiddleName");
					$row['decedent_lname'] = getDataForTag( $raw_row, "DecedentLastName");

					$dob = getDataForTag( $raw_row, "DecedentDob");
					$dod = getDataForTag( $raw_row, "DecedentDod");

					$doby = substr($dob,4);
					$dobm = substr($dob,0,2);
					$dobd = substr($dob,2,2);
					$dody = substr($dod,4);
					$dodm = substr($dod,0,2);
					$dodd = substr($dod,2,2);
					if(strlen(trim($dob))==8)
					$dob = "$doby-$dobm-$dobd";
					else
					$dob = "0000-00-00";
					if(strlen(trim($dod))==8)
					$dod = "$dody-$dodm-$dodd";
					else
					$dod = "0000-00-00";
					if($dody < "2009") continue;
					$row['decedent_dob'] = $dob;
					$row['decedent_dod'] = $dod;

					$row['FDMS_vitals_id'] = getDataForTag( $raw_row, "DecedentVitalsId" );
					$row['FDMS_case_status'] = getDataForTag( $raw_row, "DecedentCaseStatus");
					$row['FDMS_preneed_status'] = getDataForTag( $raw_row, "DecedentPreneedStatus");

					$rows[] = $row;
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getCaseDetails($vitalsId)
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectCaseByDecedentVitalsId( $vitalsId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);

				$rows = array();

				$payload = str_ireplace("\t", "", $payload);
				$payload = str_ireplace("\n", "", $payload);
				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("<datarow>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$raw_rows = array( $payload );
				foreach( $raw_rows as $raw_row )
				{
					$dos = getDataForTag( $raw_row, "DecedentServiceDate");
					$dosy = substr($dos,4);
					$dosm = substr($dos,0,2);
					$dosd = substr($dos,2,2);
					if(strlen(trim($dos))==8)
					$dos = "$dosy-$dosm-$dosd";
					else
					$dos = "0000-00-00";

					$serviceDate = new DateTime();
					$serviceDate->setDate( $dosy, $dosm, $dosd );

					$currentDate = new DateTime();
					list($cury,$curm,$curd) = explode( "-", $currentDate->format("Y-m-d"));
					$curm -= 12;

					if($curm<1)
					{
						$cury--;
						$curm += 12;
					}

					$lastTwoMonthsDate = new DateTime();
					$lastTwoMonthsDate->setDate( $cury, $curm, $curd );

					$caseStatus = getDataForTag( $raw_row, "DecedentCaseStatus");;
					$preneedStatus = getDataForTag( $raw_row, "DecedentPreneedStatus");
					$saleType = getDataForTag( $raw_row, "DecedentSaleType");
					$serviceType = getDataForTag( $raw_row, "DecedentServiceType");

					$preneedStatusCondMet = ($preneedStatus=='S' || $preneedStatus=='' || $preneedStatus==' ');
					$caseStatusCondMet = ($caseStatus=='1' || $caseStatus=='0');
					$saleTypeCondMet = ($saleType != "Trade" && $saleType != "Shipping" && $saleType != "Preneed");
					//$serviceDateCondMet = ($serviceDate >= $lastTwoMonthsDate);
					$serviceDateCondMet = true;

					$allConditionsMet = ($preneedStatusCondMet && $caseStatusCondMet && $saleTypeCondMet && $serviceDateCondMet);
					if(!$allConditionsMet)
					continue;

					$serviceDate = $dos;
					$earliest_date = "";

					$row['FDMS_case_status'] = $caseStatus;
					$row['case_preneed_status'] = $preneedStatus;
					$row['sale_type'] = $saleType;
					$row['sale_type_code'] = getDataForTag( $raw_row, "DecedentSaleTypeCode");
					$row['service_type'] = $serviceType;

					$row['FDMS_vitals_id'] = getDataForTag( $raw_row, "DecedentVitalsId");

					$row['service_time'] = getDataForTag( $raw_row, "ServiceTime");
					$row['contact_fname'] = getDataForTag( $raw_row, "ContactFirstName");
					$row['contact_mname'] = getDataForTag( $raw_row, "ContactMidName");
					$row['contact_lname'] = getDataForTag( $raw_row, "ContactLastName");
					$row['contact_address'] = getDataForTag( $raw_row, "ContactStreet");
					$row['contact_city'] = getDataForTag( $raw_row, "ContactCity");
					$row['contact_state'] = getDataForTag( $raw_row, "ContactState");
					$row['contact_zip'] = getDataForTag( $raw_row, "ContactZip");

					$row['cemetery_name'] = getDataForTag( $raw_row, "CemeteryName");
					$row['cemetery_address'] = getDataForTag( $raw_row, "CemeteryStreet");
					$row['cemetery_city'] = getDataForTag( $raw_row, "CemeteryCity");
					$row['cemetery_state'] = getDataForTag( $raw_row, "CemeteryState");
					$row['cemetery_zip'] = getDataForTag( $raw_row, "CemeteryZip");

					$row['crematory_name'] = getDataForTag( $raw_row, "CrematoryName");
					$row['crematory_street'] = getDataForTag( $raw_row, "CrematoryStreet");
					$row['crematory_city'] = getDataForTag( $raw_row, "CrematoryCity");
					$row['crematory_state'] = getDataForTag( $raw_row, "CrematoryState");
					$row['crematory_zip'] = getDataForTag( $raw_row, "CrematoryZip");

					$row['addnl_svc'] = getDataForTag( $raw_row, "AddnlService");
					$row['addnl_svc_date'] = getDataForTag( $raw_row, "AddnlServiceDate");
					$row['addnl_svc_place'] = getDataForTag( $raw_row, "AddnlServicePlace");
					$row['addnl_svc_street'] = getDataForTag( $raw_row, "AddnlServiceStreet");
					$row['addnl_svc_city'] = getDataForTag( $raw_row, "AddnlServiceCity");
					$row['addnl_svc_state'] = getDataForTag( $raw_row, "AddnlServiceState");
					$row['addnl_svc_time'] = getDataForTag( $raw_row, "AddnlServiceTime");

					$row['church'] = getDataForTag( $raw_row, "Church");
					$row['church_street'] = getDataForTag( $raw_row, "ChurchStreet");
					$row['church_citystatezip'] = getDataForTag( $raw_row, "ChurchCityStZip");

					$row['FDMS_funeral_director_id'] = getDataForTag( $raw_row, "DecedentArrangerId");
					$row['FDMS_case_num'] = getDataForTag( $raw_row, "DecedentCaseNumber");
					$row['decedent_fname'] = getDataForTag( $raw_row, "DecedentFirstName");
					$row['decedent_mname'] = getDataForTag( $raw_row, "DecedentMiddleName");
					$row['decedent_lname'] = getDataForTag( $raw_row, "DecedentLastName");

					$dob = getDataForTag( $raw_row, "DecedentDob");
					$dod = getDataForTag( $raw_row, "DecedentDod");

					$doby = substr($dob,4);
					$dobm = substr($dob,0,2);
					$dobd = substr($dob,2,2);
					$dody = substr($dod,4);
					$dodm = substr($dod,0,2);
					$dodd = substr($dod,2,2);
					if(strlen(trim($dob))==8)
					$dob = "$doby-$dobm-$dobd";
					else
					$dob = "0000-00-00";
					if(strlen(trim($dod))==8)
					$dod = "$dody-$dodm-$dodd";
					else
					$dod = "0000-00-00";
					$row['decedent_dob'] = $dob;
					$row['decedent_dod'] = $dod;

					$row['decedent_city'] = getDataForTag( $raw_row, "DecedentResidentCity");
					$row['decedent_state'] = getDataForTag( $raw_row, "DecedentResidentState");

					$row['service_date'] = $dos;

					$tos = getDataForTag( $raw_row, "ServiceTime" );

					$row['service_time'] = $tos;

					$rows[] = $row;
				}

				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getInventoryForBranchNum( $fhBranchNum )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectMerchandiseByFuneralHomeId( $fhBranchNum, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					$row['item_category'] = getDataForTag( $raw_row, "ItemCategory");
					$row['item_name'] = getDataForTag( $raw_row, "ItemName" );
					$row['line_number'] = getDataForTag( $raw_row, "ContractLineNumber");
					$row['item_description'] = getDataForTag( $raw_row, "ItemDescription");
					$row['inventory_master_id'] = getDataForTag( $raw_row, "InvMasterId");
					$row['item_amount'] = getDataForTag( $raw_row, "ItemAmount");

					$rows[] = $row;
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getCasketsAndVaultsBranchNumber( $fhBranchNum )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectMerchandiseByFuneralHomeId( $fhBranchNum, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					$itemCategory = getDataForTag( $raw_row, "ItemCategory" );
					if($itemCategory == "Casket" || $itemCategory == "Vault")
					{
						$row['item_category'] = getDataForTag( $raw_row, "ItemCategory");
						$row['item_name'] = getDataForTag( $raw_row, "ItemName" );
						$row['line_number'] = getDataForTag( $raw_row, "ContractLineNumber");
						$row['item_description'] = getDataForTag( $raw_row, "ItemDescription");
						$row['inventory_master_id'] = getDataForTag( $raw_row, "InvMasterId");
						$row['item_amount'] = getDataForTag( $raw_row, "ItemAmount");

						$rows[] = $row;
					}
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getUrnsAndVaultsForBranchNumber( $fhBranchNum )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectMerchandiseByFuneralHomeId( $fhBranchNum, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					$itemCategory = getDataForTag( $raw_row, "ItemCategory" );
					if(stripos( $itemCategory, "Urn Vault" )!==false)
					{
						$row['item_category'] = getDataForTag( $raw_row, "ItemCategory");
						$row['item_name'] = getDataForTag( $raw_row, "ItemName" );
						$row['line_number'] = getDataForTag( $raw_row, "ContractLineNumber");
						$row['item_description'] = getDataForTag( $raw_row, "ItemDescription");
						$row['inventory_master_id'] = getDataForTag( $raw_row, "InvMasterId");
						$row['item_amount'] = getDataForTag( $raw_row, "ItemAmount");

						$rows[] = $row;
					}
					else
					if(stripos( $itemCategory, "Urn" )!==false)
					{
						$row['item_category'] = getDataForTag( $raw_row, "ItemCategory");
						$row['item_name'] = getDataForTag( $raw_row, "ItemName" );
						$row['line_number'] = getDataForTag( $raw_row, "ContractLineNumber");
						$row['item_description'] = getDataForTag( $raw_row, "ItemDescription");
						$row['inventory_master_id'] = getDataForTag( $raw_row, "InvMasterId");
						$row['item_amount'] = getDataForTag( $raw_row, "ItemAmount");

						$rows[] = $row;
					}
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getServicesForLocaleId( $fhLocaleId )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectServicesByFuneralHomeLocaleId( $fhLocaleId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					// fill in rows
					$row['item_category'] = getDataForTag( $raw_row, "ItemCategory");
					$row['line_number'] = getDataForTag( $raw_row, "ContractLineNumber");
					$row['item_description'] = getDataForTag( $raw_row, "ItemDescription");
					$row['price_list_id'] = getDataForTag( $raw_row, "PriceListId");
					$row['item_seq_number'] = getDataForTag( $raw_row, "ItemSequenceNumber");
					$row['item_amount'] = getDataForTag( $raw_row, "ItemAmount");

					$itemnum = 0+$row['item_seq_number'];

					if($itemnum >= 100 && $itemnum <= 199 )
					$rows[] = $row;
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getMerchandiseForLocaleId( $fhLocaleId )
	{
		$results = array();
		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectServicesByFuneralHomeLocaleId( $fhLocaleId, FDMS_PASSWORD );
			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					// fill in rows
					$row['item_category'] = getDataForTag( $raw_row, "ItemCategory");
					$row['line_number'] = getDataForTag( $raw_row, "ContractLineNumber");
					$row['item_description'] = getDataForTag( $raw_row, "ItemDescription");
					$row['price_list_id'] = getDataForTag( $raw_row, "PriceListId");
					$row['item_seq_number'] = getDataForTag( $raw_row, "ItemSequenceNumber");
					$row['item_amount'] = getDataForTag( $raw_row, "ItemAmount");

					$itemnum = 0+$row['item_seq_number'];

					if($itemnum >= 200 && $itemnum <= 299 )
					$rows[] = $row;
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	// price list for services and merchandise.  needed so we can preload for session
	public static function getPriceListForLocaleId( $fhLocaleId )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectServicesByFuneralHomeLocaleId( $fhLocaleId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					// fill in rows
					$item_category = getDataForTag( $raw_row, "ItemCategory");
					$line_number = getDataForTag( $raw_row, "ContractLineNumber");
					$item_description = getDataForTag( $raw_row, "ItemDescription");
					$price_list_id = getDataForTag( $raw_row, "PriceListId");
					$item_seq_number = getDataForTag( $raw_row, "ItemSequenceNumber");
					$item_amount = getDataForTag( $raw_row, "ItemAmount");

					$row['item_category'] = $item_category;
					$row['line_number'] = $line_number;
					$row['item_description'] = $item_description;
					$row['price_list_id'] = $price_list_id;
					$row['item_seq_number'] = $item_seq_number;
					$row['item_amount'] = $item_amount;

					$rows[$price_list_id] = $row;
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getCashAdvancesForLocaleId( $fhLocaleId )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectServicesByFuneralHomeLocaleId( $fhLocaleId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					// fill in rows
					$row['item_category'] = getDataForTag( $raw_row, "ItemCategory");
					$row['line_number'] = getDataForTag( $raw_row, "ContractLineNumber");
					$row['item_description'] = getDataForTag( $raw_row, "ItemDescription");
					$row['price_list_id'] = getDataForTag( $raw_row, "PriceListId");
					$row['item_seq_number'] = getDataForTag( $raw_row, "ItemSequenceNumber");
					$row['item_amount'] = getDataForTag( $raw_row, "ItemAmount");
					$itemnum = 0+$row['item_seq_number'];

					if($itemnum >= 300 && $itemnum <= 399 )
					{
						$rows[] = $row;
					}
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getPackagesForLocaleId( $fhLocaleId )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectPackagesByFuneralHomeLocaleId( $fhLocaleId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					$row['package_id'] = getDataForTag( $raw_row, "PackageId");
					$row['package_status'] = getDataForTag( $raw_row, "StatusOfPackage");
					$row['package_type'] = getDataForTag( $raw_row, "PackageType");

					$rows[] = $row;
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getPackageDetailsForPackageId( $fhPackageId, $fhLocaleId )
	{
		$results = FDMSAPIcalls::getPriceListForLocaleId($fhLocaleId);
		if($results['status']!="SUCCEEDED")
		return $results;
		$priceList = $results['rows'];

		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectPackageByPackageId( $fhPackageId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);

				$rows = array();

				$payload = str_ireplace("\t", "", $payload);
				$payload = str_ireplace("\n", "", $payload);
				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("<datarow>", "<DATAROW>", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$raw_rows = explode( "<DATAROW", $payload );
				foreach( $raw_rows as $raw_row )
				{
					if(trim($raw_row)=="") continue;

					$row = array();

					$price_list_id = getDataForTag( $raw_row, "PriceListID" );
					$item_description = getDataForTag( $raw_row, "ItemDescription" );
					$item_amount = getDataForTag( $raw_row, "ItemAmount" );

					$priceListRow = $priceList[$price_list_id];
					if(isset($priceListRow))
					{
						$row['item_category'] = $priceListRow['item_category'];
						$row['line_number'] = $priceListRow['line_number'];
						$row['item_seq_number'] = $priceListRow['item_seq_number'];
					}
					else
					{
						$row['item_category'] = '';
						$row['line_number'] = '';
						$row['item_seq_number'] = '';
					}

					$row['price_list_id'] = $price_list_id;
					$row['item_description'] = $item_description;
					$row['item_amount'] = $item_amount;

					$rows[] = $row;
				}

				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getBurialTributesForLocaleId( $fhLocaleId )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectPackagesByFuneralHomeLocaleId( $fhLocaleId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					$package_type  = getDataForTag( $raw_row, "PackageType");
					if(stripos( $package_type, "Burial Tribute" ))
					{
						$row['package_id'] = getDataForTag( $raw_row, "PackageId");
						$row['package_status'] = getDataForTag( $raw_row, "StatusOfPackage");
						$row['package_type'] = getDataForTag( $raw_row, "PackageType");
						$rows[] = $row;
					}
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getCremationTributesForLocaleId( $fhLocaleId )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectPackagesByFuneralHomeLocaleId( $fhLocaleId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;

					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					$package_type  = getDataForTag( $raw_row, "PackageType");
					if(strpos( $package_type, "Cremation Tribute" ))
					{
						$row['package_id'] = getDataForTag( $raw_row, "PackageId");
						$row['package_status'] = getDataForTag( $raw_row, "StatusOfPackage");
						$row['package_type'] = getDataForTag( $raw_row, "PackageType");

						$rows[] = $row;
					}
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getMemorialTributesForLocaleId( $fhLocaleId )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectPackagesByFuneralHomeLocaleId( $fhLocaleId, FDMS_PASSWORD );
			// take the payload out of the SOAP envelope

			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					$package_type  = getDataForTag( $raw_row, "PackageType");
					if(stripos( $package_type, "Memorial" ))
					{
						$row['package_id'] = getDataForTag( $raw_row, "PackageId");
						$row['package_status'] = getDataForTag( $raw_row, "StatusOfPackage");
						$row['package_type'] = getDataForTag( $raw_row, "PackageType");
						$rows[] = $row;
					}
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getOtherOfferingsForLocaleId( $fhLocaleId )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectPackagesByFuneralHomeLocaleId( $fhLocaleId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$row = array();

					$package_type  = getDataForTag( $raw_row, "PackageType");
					if(!stripos( $package_type, "Tribute" ))
					{
						$pkg_type = getDataForTag( $raw_row, "PackageType" );
						if( $pkg_type != "Trade" &&
						$pkg_type != "Shipping" &&
						$pkg_type != "Monuments and Markers" )
						{
							$row['package_id'] = getDataForTag( $raw_row, "PackageId");
							$row['package_status'] = getDataForTag( $raw_row, "StatusOfPackage");
							$row['package_type'] = getDataForTag( $raw_row, "PackageType");

							$rows[] = $row;
						}
					}
				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['rows'] = $rows;
			}
		}

		return $results;
	}

	public static function getPaymentOptionsForLocaleId( $fhLocaleId )
	{
		$results = array();

		$soapClient = new SoapClient( FDMS_WSDL );
		if(null == $soapClient)
		{
			$results['status'] = "FAIL";
			$results['reason'] = "Cannot instantiate SOAP client";
		}
		else
		{
			$rawxml = $soapClient->SelectPaymentMethodByFuneralHomeLocaleId( $fhLocaleId, FDMS_PASSWORD );

			// take the payload out of the SOAP envelope
			$idx1 = stripos( $rawxml, "<data_structure>" );
			$idx2 = stripos( $rawxml, "</data_structure>" );
			if($idx1 >= $idx2)
			{
				$results['status'] = "FAIL";
				$results['reason'] = "Malformed response (or response has been altered).";
			}
			else
			{
				$payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
				$payload = str_replace( "&", "and", $payload );

				$payload = str_ireplace("<data_structure>", "", $payload);
				$payload = str_ireplace("</data_structure>", "", $payload);
				$payload = str_ireplace("</datarow>", "", $payload);

				$rows = array();
				$methods = array();

				$count = 0;
				$raw_rows = explode( "<DATAROW>", $payload );
				foreach( $raw_rows as $raw_row )
				{
					$trimmed_row = $raw_row;
					if(empty($trimmed_row))
					{
						echo "skipping";
						continue;
					}

					$payment_combo  = getDataForTag( $raw_row, "PaymentMethod");

					list($abbrev,$name) = explode("-", $payment_combo);
					if(!empty($abbrev)) {
						$methods[$abbrev] = $name;
					}

				}

				$results['status'] = "SUCCEEDED";
				$results['reason'] = "";
				$results['payment_methods'] = $methods;
			}
		}

		return $results;
	}
}
?>
